package com.block.channel.flow.channel;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.channel.flow.IChannelReportService;
import com.block.channel.model.result.ReprotFlowResult;
import com.block.common.bean.CommonParam;
import com.block.common.service.LogFactory;
import com.block.db.entity.ChannelBaseInfo;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class XiangDianChannelReportService implements IChannelReportService {

    private static Logger log = LogFactory.getCommLog();

    private String requestUrl = "https://oicount.adexplain.com/v2/action/monitor";

    @Override
    public ReprotFlowResult registerActionReport(CommonParam commonParam, ChannelBaseInfo channelBaseInfo) {
        ReprotFlowResult flowResult = new ReprotFlowResult();
        flowResult.setSuccess(false);

        JSONObject headObject = commonParam.getHeadObject();
        String landingid = headObject.getString("landingid");
        if (StrUtil.isEmpty(landingid)){
            flowResult.setCode("-2");
            flowResult.setMessage("landingid参数为空，不做处理");
            return flowResult;
        }

        Map<String, Object> bizMap = new HashMap<>();
        bizMap.put("landingid", landingid);
        bizMap.put("exposure", 0);

        log.info("【享点上报】【注册】【开始请求】【reqestUrl】{}【bizMap】{}", requestUrl, JSON.toJSONString(bizMap));
        String response = HttpUtil.createGet(requestUrl)
                .form(bizMap)
                .execute().body();
        log.info("【享点上报】【注册】【开始结束】【result】{}", response);

        JSONObject jsonObject = JSONObject.parseObject(response);

        boolean isSuccessed = jsonObject.getBoolean("isSuccessed");
        String errorCode = jsonObject.getString("errorCode");
        String errorDesc = jsonObject.getString("errorDesc");

        flowResult.setCode(errorCode);
        flowResult.setMessage(errorDesc);
        flowResult.setParamInfo(JSON.toJSONString(bizMap));
        flowResult.setResultInfo(response);
        if (isSuccessed){
            flowResult.setSuccess(true);
        }
        return flowResult;
    }
    @Override
    public ReprotFlowResult sourceActionReport(CommonParam commonParam, ChannelBaseInfo channelBaseInfo) {
        return null;
    }
    @Override
    public ReprotFlowResult downloadActionReport(CommonParam commonParam, ChannelBaseInfo channelBaseInfo) {
        return null;
    }

    @Override
    public ReprotFlowResult payBuyActionReport(CommonParam commonParam, ChannelBaseInfo channelBaseInfo) {
        return null;
    }


}
